package com.wxpay.config;

import java.util.HashMap;
import java.util.Map;

import com.wxpay.config.WxPayApi.TradeType;
import com.wxpay.util.WXPayUtil;

import hyl.core.MyFun;


public class WxPayConf {
	private String appId;
	private String mchId;
	private String subAppId;
	private String subMchId;
	private String paternerKey;
	private String nonceStr;
	private String body;
	private String attach;
	private String transactionId;
	private String outTradeNo;
	private String totalFee;
	private String spbillCreateIp;
	private String notifyUrl;
	private TradeType tradeType;
	private String openId;
	private String subOpenId;
	private String authCode;
	private String sceneInfo;
	
	public Map<String,String> Build() throws Exception{
		Map<String, String> map =new HashMap<String, String>();
		if(getTradeType().equals(TradeType.JSAPI)) {
			System.out.println("公众号支付...");
			map.put("openid", getOpenId());
		}
		/**
		 * H5支付必填scene_info
		 */
		if (getTradeType().equals(TradeType.MWEB)) {
			if (MyFun.isEmpty(getSceneInfo())) {
				throw new IllegalArgumentException("微信H5支付 scene_info 不能同时为空");
			}
			map.put("scene_info", getSceneInfo());
		}
		
		
		map.put("appid", getAppId());
		map.put("mch_id", getMchId());
		map.put("nonce_str", getNonceStr());
		map.put("body", getBody());
		map.put("out_trade_no", getOutTradeNo());
		map.put("total_fee", getTotalFee());
		map.put("spbill_create_ip", getSpbillCreateIp());
		
		map.put("trade_type", getTradeType().name());
		map.put("out_trade_no", getOutTradeNo());
		
		//map.put("attach", getAttach());
		if (getTradeType().equals(TradeType.MICROPAY)) {
			map.put("auth_code", getAuthCode());
			map.remove("trade_type");
		}else {
			map.put("notify_url", getNotifyUrl());
		}
		
		map.put("sign", WXPayUtil.generateSignature(map, getPaternerKey()));
		
		return map;
	}
	public Map<String,String> BuildPt() throws Exception{
		Map<String, String> map =new HashMap<String, String>();
		if(getTradeType().equals(TradeType.JSAPI)) {
			System.out.println("公众号支付...");
			/*if (MyFun.isEmpty(getSubAppId())||subAppId==null) {
				map.put("sub_appid", subAppId);
				map.put("sub_openid", getSubOpenId());
			}else {
				map.put("openid", getOpenId());
			}*/
			map.put("openid", getOpenId());
		}
		/**
		 * H5支付必填scene_info
		 */
		if (getTradeType().equals(TradeType.MWEB)) {
			if (MyFun.isEmpty(getSceneInfo())) {
				throw new IllegalArgumentException("微信H5支付 scene_info 不能同时为空");
			}
			map.put("scene_info", getSceneInfo());
		}
		
		
		map.put("appid", getAppId());
		map.put("mch_id", getMchId());
		map.put("sub_appid", getSubAppId());
		map.put("sub_mch_id", getSubMchId());
		map.put("nonce_str", getNonceStr());
		map.put("body", getBody());
		map.put("out_trade_no", getOutTradeNo());
		map.put("total_fee", getTotalFee());
		map.put("spbill_create_ip", getSpbillCreateIp());
		
		map.put("trade_type", getTradeType().name());
		map.put("out_trade_no", getOutTradeNo());
		
		//map.put("attach", getAttach());
		if (getTradeType().equals(TradeType.MICROPAY)) {
			map.put("auth_code", getAuthCode());
			map.remove("trade_type");
		}else {
			map.put("notify_url", getNotifyUrl());
		}
		
		map.put("sign", WXPayUtil.generateSignature(map, getPaternerKey()));
		
		return map;
	}
	public String getAppId() {
		if (MyFun.isEmpty(appId))
			throw new IllegalArgumentException("appId 未赋值");
		return appId;
	}

	public void setAppId(String appId) {
		if (MyFun.isEmpty(appId))
			throw new IllegalArgumentException("appId 不能为空");
		this.appId = appId;
	}

	public String getMchId() {
		if (MyFun.isEmpty(mchId))
			throw new IllegalArgumentException("mchId 未赋值");
		return mchId;
	}

	public void setMchId(String mchId) {
		if (MyFun.isEmpty(mchId))
			throw new IllegalArgumentException("mchId 不能为空");
		this.mchId = mchId;
	}

	public String getSubAppId() {
		return subAppId;
	}

	public void setSubAppId(String subAppId) {
		if (MyFun.isEmpty(subAppId))
			throw new IllegalArgumentException("subAppId 不能为空");
		this.subAppId = subAppId;
	}

	public String getSubMchId() {
		if (MyFun.isEmpty(subMchId))
			throw new IllegalArgumentException("subMchId 未赋值");
		return subMchId;
	}

	public void setSubMchId(String subMchId) {
		if (MyFun.isEmpty(subMchId))
			throw new IllegalArgumentException("subMchId 不能为空");
		this.subMchId = subMchId;
	}

	public String getPaternerKey() {
		if (MyFun.isEmpty(paternerKey))
			throw new IllegalArgumentException("paternerKey 未赋值");
		return paternerKey;
	}

	public void setPaternerKey(String paternerKey) {
		if (MyFun.isEmpty(paternerKey))
			throw new IllegalArgumentException("paternerKey 不能为空");
		this.paternerKey = paternerKey;
	}

	public String getNonceStr() {
		if (MyFun.isEmpty(nonceStr))
			nonceStr = String.valueOf(System.currentTimeMillis());
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		if (MyFun.isEmpty(body))
			this.nonceStr = String.valueOf(System.currentTimeMillis());
		this.nonceStr = nonceStr;
	}

	public String getBody() {
		if (MyFun.isEmpty(body))
			throw new IllegalArgumentException("body 未赋值");
		return body;
	}

	public void setBody(String body) {
		if (MyFun.isEmpty(body))
			throw new IllegalArgumentException("body 不能为空");
		this.body = body;
	}

	public String getAttach() {
		return attach;
	}

	public void setAttach(String attach) {
		if (MyFun.isEmpty(attach))
			throw new IllegalArgumentException("attach 不能为空");
		this.attach = attach;
	}

	public String getTransactionId() {
		if (MyFun.isEmpty(transactionId))
			throw new IllegalArgumentException("transactionId 未赋值");
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		if (MyFun.isEmpty(transactionId))
			throw new IllegalArgumentException("transactionId 不能为空");
		this.transactionId = transactionId;
	}

	public String getOutTradeNo() {
		if (MyFun.isEmpty(outTradeNo))
			throw new IllegalArgumentException("outTradeNo 未赋值");
		return outTradeNo;
	}

	public void setOutTradeNo(String outTradeNo) {
		if (MyFun.isEmpty(outTradeNo))
			throw new IllegalArgumentException("outTradeNo 不能为空");
		this.outTradeNo = outTradeNo;
	}

	public String getTotalFee() {
		if (MyFun.isEmpty(totalFee))
			throw new IllegalArgumentException("totalFee 未赋值");
		return totalFee;
	}

	public void setTotalFee(String totalFee) {
		if (MyFun.isEmpty(totalFee))
			throw new IllegalArgumentException("totalFee 不能为空");
		this.totalFee = totalFee;
	}

	public String getSpbillCreateIp() {
		if (MyFun.isEmpty(spbillCreateIp))
			throw new IllegalArgumentException("spbillCreateIp 未赋值");
		return spbillCreateIp;
	}

	public void setSpbillCreateIp(String spbillCreateIp) {
		if (MyFun.isEmpty(spbillCreateIp))
			throw new IllegalArgumentException("spbillCreateIp 不能为空");
		this.spbillCreateIp = spbillCreateIp;
	}

	public String getNotifyUrl() {
		if (MyFun.isEmpty(notifyUrl))
			throw new IllegalArgumentException("notifyUrl 未赋值");
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		if (MyFun.isEmpty(notifyUrl))
			throw new IllegalArgumentException("notifyUrl 不能为空");
		this.notifyUrl = notifyUrl;
	}

	public TradeType getTradeType() {
		if (tradeType==null)
			throw new IllegalArgumentException("tradeType 未赋值");
		return tradeType;
	}

	public void setTradeType(TradeType tradeType) {
		if (tradeType==null)
			throw new IllegalArgumentException("tradeType 不能为空");
		this.tradeType = tradeType;
	}

	public String getOpenId() {
		if (MyFun.isEmpty(openId))
			throw new IllegalArgumentException("openId 未赋值");
		return openId;
	}

	public void setOpenId(String openId) {
		if (MyFun.isEmpty(openId))
			throw new IllegalArgumentException("openId 不能为空");
		this.openId = openId;
	}

	public String getSubOpenId() {
		if (MyFun.isEmpty(subOpenId))
			throw new IllegalArgumentException("subOpenId 未赋值");
		return subOpenId;
	}

	public void setSubOpenId(String subOpenId) {
		if (MyFun.isEmpty(subOpenId))
			throw new IllegalArgumentException("subOpenId 不能为空");
		this.subOpenId = subOpenId;
	}

	public String getAuthCode() {
		if (MyFun.isEmpty(authCode))
			throw new IllegalArgumentException("authCode 未赋值");
		return authCode;
	}

	public void setAuthCode(String authCode) {
		if (MyFun.isEmpty(authCode))
			throw new IllegalArgumentException("authCode 不能为空");
		this.authCode = authCode;
	}

	public String getSceneInfo() {
		return sceneInfo;
	}

	public void setSceneInfo(String sceneInfo) {
		if (MyFun.isEmpty(sceneInfo))
			throw new IllegalArgumentException("sceneInfo 不能为空");
		this.sceneInfo = sceneInfo;
	}

}
